package org.snia.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 静态加载Properties配置文件
 * 
 * @author c90003207
 * 
 */
public class ConfigurableConstants {
	
	/**
	 * 记录日志的工具变量
	 */
	protected static Log logger = LogFactory
			.getLog(ConfigurableConstants.class);
	
	/**
	 * 属性保存工具
	 */
	protected static Properties p = new Properties();

	/**
	 * 初始化加载文件
	 * 
	 * @param propertyFileName
	 */
	protected static void init(String propertyFileName) {
		InputStream in = null;
		try {
			in = ConfigurableConstants.class.getClassLoader()
					.getResourceAsStream(propertyFileName);
			if (in != null)
				p.load(in);
		} catch (IOException e) {
			logger.error("load " + propertyFileName + " into Constants error!",
					e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					logger.error("close " + propertyFileName + " error!", e);
				}
			}
		}
	}

	/**
	 * 根据key值取value
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	protected static String getProperty(String key, String defaultValue) {
		return p.getProperty(key, defaultValue);
	}
}